home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / tty_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  2.9 KB  |  121 lines

  1. #include "util.h"
  2. #include <pwd.h>
  3. #include <sys/stat.h>
  4. #include <utmp.h>
  5.  
  6. extern int errno;
  7. extern jmp_buf timerest;
  8. extern char *blt();
  9.  
  10. LOCFUN tty_open();
  11.  
  12. FILE *tty_fp;                     /* user sends tty output to this      */
  13.  
  14. /*  ****************  (tty_) ACCESS USERS' TTYS  ********************** */
  15.  
  16. tty_init ()                       /* setup for ttyecipient's tty        */
  17. {
  18.     setutmp ();                   /* get the utmp file                  */
  19.     return (OK);
  20. }
  21.  
  22. tty_end ()                       /* done with tty access               */
  23. {
  24.     endutmp ();                   /* done with utmp package             */
  25.     return (OK);
  26. }
  27.  
  28. tty_find (usrname)                /* find & open recipient's tty        */
  29. char    usrname[];
  30. {
  31.     extern struct utmp *getutnam ();
  32.     static char tty_num[14] = "/dev/";
  33.                   /* number of tty receiver is on       */
  34.                   /* name is filled in from getutnam    */
  35.     struct utmp *utptr;
  36.  
  37. retry:
  38.     if ((utptr = getutnam (usrname)) == 0)
  39.     {
  40.     errno = ENOENT;           /* not logged in                      */
  41.     return (NOTOK);
  42.     }
  43. #ifdef V4_2BSD
  44.     /*
  45.      * do not return the ttyname when it is a pty and not used for a
  46.      * real login session (like windows)
  47.      */
  48. #ifndef nonuser
  49. #define nonuser(ut)    ((ut).ut_host[0] == 0 && \
  50.     strncmp((ut).ut_line, "tty", 3) == 0 && ((ut).ut_line[3] == 'p' \
  51.     || (ut).ut_line[3] == 'q' || (ut).ut_line[3] == 'r'))
  52. #endif
  53.     if (nonuser(*utptr))
  54.     goto retry;
  55. #endif
  56.     blt (utptr -> ut_line, &tty_num[5], 8);
  57.  
  58.     return (tty_open (tty_num));
  59. }
  60. /* */
  61.  
  62. LOCFUN
  63.     tty_open (tty_num)        /* open the named tty                 */
  64. char tty_num[];
  65. {
  66.     struct stat statbuf;
  67.     int tty_fd;
  68.  
  69.     if (setjmp (timerest))   /* signal, for timerest, must be      */
  70.     {                             /*    set by user program             */
  71.     errno = EBUSY;            /* allow a later try                  */
  72.     return (NOTOK);           /* timeout during tty open            */
  73.     }
  74.  
  75. /*
  76.  * Vanilla 4.3BSD uses setgid-to-group-"tty" programs to do secure
  77.  * writing on peoples' terminals.
  78.  *
  79.  * Oh, for #elif's in /lib/cpp.
  80.  * -- David Herron <david@ms.uky.csnet>, 18-Mar-87
  81.  */
  82. #if !defined(SECURETTY) && !defined(V4_3BSD)
  83.     if (stat (tty_num, &statbuf) < OK)
  84.     return (NOTOK);
  85.  
  86.     if (!(statbuf.st_mode & (S_IWRITE >> 6)))
  87. #else
  88. #ifdef SECURETTY
  89.     if (access (tty_num, 01) != 0)
  90. #endif
  91. #if defined(V4_3BSD) && !defined(SECURETTY)
  92.     if (stat (tty_num, &statbuf) < OK)
  93.     return (NOTOK);
  94.  
  95.     if (!(statbuf.st_mode & (S_IWRITE >> 3)))
  96. #endif
  97. #endif
  98.     {                             /* check write(I) permissions      */
  99.     errno = EBUSY;
  100.     return (NOTOK);
  101.     }
  102.  
  103.     s_alarm (15);
  104.     tty_fd = open (tty_num, 1);
  105.     s_alarm (0);
  106.  
  107.     if (tty_fd < 0)
  108.     return (NOTOK);
  109.  
  110.     tty_fp = fdopen (tty_fd, "w");
  111.     return (OK);
  112. }
  113.  
  114.  
  115. tty_close ()              /* done writing to tty                */
  116. {
  117.     if (ferror (tty_fp) || fclose (tty_fp) == EOF)
  118.     return (NOTOK);
  119.     return (OK);
  120. }
  121.